import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yi_shop/model/message.dart';
import 'package:yi_shop/ui/scroll_refresh_config.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/svg.dart';
import 'package:yi_shop/utils/tools.dart';

class MessageTradeController extends GetxController {
  RxList<MessageTradeModel> pageList = RxList([]);

  RefreshController refreshController = RefreshController();

  @override
  void onInit() {
    pageList.add(MessageTradeModel(
        title: '退款提醒：你的46.95元退款进度有更新！',
        date: '19/5/14',
        remark: '交通银行银行卡（6821）入账时间2019-05-20 22:00，若超时未收到，请联系银行。'));
    pageList.add(MessageTradeModel(
        title: '退款提醒：你的46.95元退款进度有更新！',
        date: '19/5/14',
        remark: '交通银行银行卡（6821）入账时间2019-05-20 22:00，若超时未收到，请联系银行。'));
    pageList.add(MessageTradeModel(
        title: '退款提醒：你的46.95元退款进度有更新！',
        date: '19/5/14',
        remark: '交通银行银行卡（6821）入账时间2019-05-20 22:00，若超时未收到，请联系银行。'));
    pageList.add(MessageTradeModel(
        title: '退款已经成功', date: '19/5/14', remark: '亲爱的"ldqxh"：退款售后编号：2632953926121460，卖家已经给您退款。'));
    pageList.add(MessageTradeModel(
        title: '包裹已领取',
        date: '19/5/14',
        remark: '您的包裹已成功取出，您可点击对我们的服务进行评价>> \n运单编号：3836071961080'));
    pageList.add(MessageTradeModel(
        title: '卖家同意协议，需要您退货',
        date: '19/5/14',
        remark: '亲爱的"ldqxh"：退款售后编号：2632953926121460，卖家已同意您退货，请在05月25日前将货物退回。'));
    super.onInit();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 35) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    initData();

    /// 完毕
    refreshController.loadComplete();
  }

  void initData() {
    for (var i = 0; i < 10; i++) {
      pageList.add(MessageTradeModel(
          title: '卖家同意协议，需要您退货',
          date: '19/5/14',
          remark: '亲爱的"ldqxh"：退款售后编号：2632953926121460，卖家已同意您退货，请在05月25日前将货物退回。'));
    }
  }
}

class MessageTradePage extends GetView<MessageTradeController> {
  const MessageTradePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: Text(
          '交易物流',
          style: StylesUtils.customTextStyle(
              fontSize: StylesUtils.fontSize18, fontWeight: FontWeight.w700),
        ),
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  ScrollRefreshConfig scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (_, int index) {
        var row = controller.pageList[index];
        return Container(
          margin: const EdgeInsets.only(top: 15),
          alignment: Alignment.center,
          child: Container(
            width: 345,
            padding: EdgeInsets.all(12.w),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(5.r),
            ),
            child: Column(
              children: [
                Container(
                  alignment: Alignment.centerLeft,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Expanded(
                        child: Text(
                          row.title,
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          style: StylesUtils.customTextStyle(
                              fontWeight: FontWeight.w500, color: ColorsUtils.color3333),
                        ),
                      ),
                      Text(
                        row.date,
                        style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                      )
                    ],
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(top: 10),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Container(
                        width: 70,
                        height: 70,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFbb8),
                          borderRadius: BorderRadius.circular(5.r),
                        ),
                        child: SvgUtils.assetName('u962', width: 30, height: 22),
                      ),
                      Expanded(
                          child: Container(
                        padding: const EdgeInsets.only(left: 10),
                        child: Text(
                          row.remark,
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color6666,
                              fontSize: StylesUtils.fontSize13,
                              lineHeight: 1.35.h),
                        ),
                      ))
                    ],
                  ),
                )
              ],
            ),
          ),
        );
      },
      itemCount: controller.pageList.length,
    );
  }
}
